package com.atguigu.qqzone.controller;

import com.atguigu.qqzone.pojo.Reply;
import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.qqzone.service.impl.ReplyServiceImpl;
import com.atguigu.qqzone.service.impl.TopicServiceImpl;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: TopicController
 * @Description:
 * @Author: wty
 * @Date: 2022/12/10
 */

public class TopicController {
    private TopicServiceImpl topicServiceImpl;

    public String topicDetail(Integer id, HttpSession session) {
        Topic topic = topicServiceImpl.getTopicById(id);

        //List<Reply> replyList = replyServiceImpl.getReplyListByTopicId(id);
        //topic.setReplyList(replyList);

        session.setAttribute("topic", topic);
        return "frames/detail";
    }

    public String delTopic(Integer topicId) {
        topicServiceImpl.delTopic(topicId);
        return "redirect:topic.do?operate=getTopicList";
    }

    public String getTopicList(HttpSession session) {
        // 从session中获取当前用户信息
        UserBasic userBasic = (UserBasic) session.getAttribute("userBasic");
        // 再次查询当前用户关联的所有日志
        List<Topic> topicList = topicServiceImpl.getTopicList(userBasic);

        // 设置关联的日志列表
        /**
         * <tr th:unless="${#lists.isEmpty(session.friend.topicList)}" th:each="topic : ${session.friend.topicList}">
         */
        // 重新覆盖
        userBasic.setTopicList(topicList);
        session.setAttribute("friend", userBasic);
        return "frames/main";
    }

    public String addTopic(String title, String content, HttpSession session) {
        UserBasic userBasic = (UserBasic) session.getAttribute("userBasic");
        Topic topic = new Topic(title, content, new Date(), new UserBasic(userBasic.getId()));
        topicServiceImpl.addTopic(topic);
        return "redirect:topic.do?operate=getTopicList";
    }

}
